<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="eloquent-集合" tabindex="-1"><a class="header-anchor" href="#eloquent-集合"><span>Eloquent: 集合</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a></li>
<li><a href="#available-methods">可用的方法</a></li>
<li><a href="#custom-collections">自定义集合</a></li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p>所有返回一个以上模型结果的 Eloquent 方法都会返回 <code v-pre>Illuminate\Database\Eloquent\Collection</code> 类的实例，包括通过 <code v-pre>get</code> 方法获取或者通过关联访问的结果。 Eloquent 集合对象扩展了 Laravel 的 <a href="https://learnku.com/docs/laravel/11.x/collections" target="_blank" rel="noopener noreferrer">基本集合</a>, 所以它很自然地继承了数十种 Eloquent 模型底层方法。可以查看 Laravel 集合文档，了解这些有用的方法！</p>
<p>所有集合还可以作为迭代器使用，可以像使用 PHP 数组一样对它们进行循环：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'active'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$users</span> <span class="token keyword">as</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">echo</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>不过，如前所述，集合比数组强大得多，它提供了各种映射/还原操作，可以使用直观的界面进行连锁操作。例如，我们可以删除所有不活动的模型，然后收集每个剩余用户的名字：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$names</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">reject</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">User</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">active</span> <span class="token operator">===</span> <span class="token constant boolean">false</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">map</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">User</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="eloquent-集合转换" tabindex="-1"><a class="header-anchor" href="#eloquent-集合转换"><span>Eloquent 集合转换</span></a></h4>
<p>大多数 Eloquent 集合方法都会返回一个 Eloquent 集合的新实例, 而 <code v-pre>collapse</code>, <code v-pre>flatten</code>, <code v-pre>flip</code>, <code v-pre>keys</code>, <code v-pre>pluck</code>, 和 <code v-pre>zip</code> 方法则会返回一个<a href="https://learnku.com/docs/laravel/11.x/collections" target="_blank" rel="noopener noreferrer">基础集合</a> 实例。 同样, 如果 <code v-pre>map</code> 操作返回的集合不包含任何 Eloquent 模型，它将被转换为基础集合实例。</p>
<h2 id="可用的方法" tabindex="-1"><a class="header-anchor" href="#可用的方法"><span>可用的方法</span></a></h2>
<p>所有 Eloquent 的集合都继承了 <a href="https://learnku.com/docs/laravel/11.x/collectionsmd#available-methods" target="_blank" rel="noopener noreferrer">Laravel collection</a> 对象；因此， 他们也继承了所有集合基类提供的强大的方法。</p>
<p>另外， <code v-pre>Illuminate\Database\Eloquent\Collection</code> 类提供了一套上层的方法来帮你管理你的模型集合。大多数方法返回 <code v-pre>Illuminate\Database\Eloquent\Collection</code> 实例；然而，也会有一些方法， 例如 <code v-pre>modelKeys</code>， 它们会返回 <code v-pre>Illuminate\Support\Collection</code> 类的实例。</p>
<h4 id="append-attributes" tabindex="-1"><a class="header-anchor" href="#append-attributes"><span><code v-pre>append($attributes)</code></span></a></h4>
<p>可以使用 <code v-pre>append</code> 方法来为集合中的模型<a href="https://learnku.com/docs/laravel/11.x/eloquent-serializationmd#appending-values-to-json" target="_blank" rel="noopener noreferrer">追加属性</a>。 该方法接受一个数组追加属性或追加单个属性：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'team'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">append</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'team'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'is_admin'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="contains-key-operator-null-value-null" tabindex="-1"><a class="header-anchor" href="#contains-key-operator-null-value-null"><span><code v-pre>contains($key, $operator = null, $value = null)</code></span></a></h4>
<p><code v-pre>contains</code> 方法可用于判断集合中是否包含指定的模型实例。这个方法接收一个主键或者模型实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="diff-items" tabindex="-1"><a class="header-anchor" href="#diff-items"><span><code v-pre>diff($items)</code></span></a></h4>
<p><code v-pre>diff</code> 方法返回不在给定集合中的所有模型：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$users</span> <span class="token operator">=</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">diff</span><span class="token punctuation">(</span><span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">whereIn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="except-keys" tabindex="-1"><a class="header-anchor" href="#except-keys"><span><code v-pre>except($keys)</code></span></a></h4>
<p><code v-pre>except</code> 方法返回给定主键外的所有模型：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token variable">$users</span> <span class="token operator">=</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">except</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="find-key" tabindex="-1"><a class="header-anchor" href="#find-key"><span><code v-pre>find($key)</code></span></a></h4>
<p><code v-pre>find</code> 方法查找给定主键的模型。如果 <code v-pre>$key</code> 是一个模型实例， <code v-pre>find</code> 将会尝试返回与主键匹配的模型。 如果 <code v-pre>$key</code> 是一个关联数组， <code v-pre>find</code> 将返回所有数组主键匹配的模型：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$user</span> <span class="token operator">=</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="fresh-with" tabindex="-1"><a class="header-anchor" href="#fresh-with"><span><code v-pre>fresh($with = [])</code></span></a></h4>
<p><code v-pre>fresh</code> 方法用于从数据库中检索集合中每个模型的新实例。此外，还将预加载任何指定的关联关系：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token variable">$users</span> <span class="token operator">=</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">fresh</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$users</span> <span class="token operator">=</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">fresh</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'comments'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="intersect-items" tabindex="-1"><a class="header-anchor" href="#intersect-items"><span><code v-pre>intersect($items)</code></span></a></h4>
<p><code v-pre>intersect</code> 方法返回给定集合与当前模型的交集：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$users</span> <span class="token operator">=</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">intersect</span><span class="token punctuation">(</span><span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">whereIn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="load-relations" tabindex="-1"><a class="header-anchor" href="#load-relations"><span><code v-pre>load($relations)</code></span></a></h4>
<p><code v-pre>load</code> 方法为集合中的所有模型加载给定关联关系：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">load</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'comments'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'posts'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">load</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'comments.author'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">load</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'comments'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'posts'</span> <span class="token operator">=></span> <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token variable">$query</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$query</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'active'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="loadmissing-relations" tabindex="-1"><a class="header-anchor" href="#loadmissing-relations"><span><code v-pre>loadMissing($relations)</code></span></a></h4>
<p>如果尚未加载关联关系，则 <code v-pre>loadMissing</code> 方法将加载集合中所有模型的给定关联关系：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">loadMissing</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'comments'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'posts'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">loadMissing</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'comments.author'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">loadMissing</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'comments'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'posts'</span> <span class="token operator">=></span> <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token variable">$query</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$query</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'active'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="modelkeys" tabindex="-1"><a class="header-anchor" href="#modelkeys"><span><code v-pre>modelKeys()</code></span></a></h4>
<p><code v-pre>modelKeys</code> 方法返回集合中所有模型的主键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">modelKeys</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// [1, 2, 3, 4, 5]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="makevisible-attributes" tabindex="-1"><a class="header-anchor" href="#makevisible-attributes"><span><code v-pre>makeVisible($attributes)</code></span></a></h4>
<p><code v-pre>makeVisible</code> 方法针对集合中平常「隐藏」的属性进行<a href="https://learnku.com/docs/laravel/11.x/eloquent-serializationmd#hiding-attributes-from-json" target="_blank" rel="noopener noreferrer">模型隐藏属性可见</a> ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token variable">$users</span> <span class="token operator">=</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">makeVisible</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'address'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'phone_number'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="makehidden-attributes" tabindex="-1"><a class="header-anchor" href="#makehidden-attributes"><span><code v-pre>makeHidden($attributes)</code></span></a></h4>
<p><code v-pre>makeHidden</code> 方法针对集合中平常「可见」的属性进行<a href="https://learnku.com/docs/laravel/11.x/eloquent-serializationmd#hiding-attributes-from-json" target="_blank" rel="noopener noreferrer">模型属性隐藏</a> ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token variable">$users</span> <span class="token operator">=</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">makeHidden</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'address'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'phone_number'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="only-keys" tabindex="-1"><a class="header-anchor" href="#only-keys"><span><code v-pre>only($keys)</code></span></a></h4>
<p><code v-pre>only</code> 方法返回具有给定主键的所有模型：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token variable">$users</span> <span class="token operator">=</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">only</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="setvisible-attributes" tabindex="-1"><a class="header-anchor" href="#setvisible-attributes"><span><code v-pre>setVisible($attributes)</code></span></a></h4>
<p><code v-pre>setVisible</code> 方法<a href="https://learnku.com/docs/laravel/11.x/eloquent-serializationmd#temporarily-modifying-attribute-visibility" target="_blank" rel="noopener noreferrer">临时覆盖</a>集合中每个模型的所有可见属性：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token variable">$users</span> <span class="token operator">=</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">setVisible</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="sethidden-attributes" tabindex="-1"><a class="header-anchor" href="#sethidden-attributes"><span><code v-pre>setHidden($attributes)</code></span></a></h4>
<p><code v-pre>setHidden</code> 方法<a href="https://learnku.com/docs/laravel/11.x/eloquent-serializationmd#temporarily-modifying-attribute-visibility" target="_blank" rel="noopener noreferrer">临时覆盖</a>集合中每个模型的所有隐藏属性:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token variable">$users</span> <span class="token operator">=</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">setHidden</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'password'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'remember_token'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="toquery" tabindex="-1"><a class="header-anchor" href="#toquery"><span><code v-pre>toQuery()</code></span></a></h4>
<p><code v-pre>toQuery</code> 方法返回一个 Eloquent 查询生成器实例，该实例包含集合模型主键上的 <code v-pre>whereIn</code> 约束：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'status'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'VIP'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">toQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">update</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'status'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Administrator'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="unique-key-null-strict-false" tabindex="-1"><a class="header-anchor" href="#unique-key-null-strict-false"><span><code v-pre>unique($key = null, $strict = false)</code></span></a></h4>
<p><code v-pre>unique</code> 方法返回集合中所有不重复的模型，若模型在集合中存在相同类型且相同主键的另一模型，该模型将被从集合中删除：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token variable">$users</span> <span class="token operator">=</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="自定义集合" tabindex="-1"><a class="header-anchor" href="#自定义集合"><span>自定义集合</span></a></h2>
<p>如果你想在操作某个模型时使用自定义的 <code v-pre>Collection</code> 对象，你可以在模型上定义一个 <code v-pre>newCollection</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Models</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>UserCollection</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Eloquent<span class="token punctuation">\</span>Collection</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Eloquent<span class="token punctuation">\</span>Model</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Model</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 创建一个新的合集实例。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">array</span></span>&lt;int, \Illuminate\Database\Eloquent\Model>  $models</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token punctuation">\</span>Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Eloquent<span class="token punctuation">\</span>Collection</span>&lt;int, \Illuminate\Database\Eloquent\Model></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">newCollection</span><span class="token punctuation">(</span><span class="token keyword type-hint">array</span> <span class="token variable">$models</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">Collection</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">UserCollection</span><span class="token punctuation">(</span><span class="token variable">$models</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦定义了 <code v-pre>newCollection</code> 方法，每当 Eloquent 返回一个 <code v-pre>Illuminate\Database\Eloquent\Collection</code> 实例时，你将得到一个你的自定义集合实例。如果你想为应用程序中的所有模型使用自定义集合，你应该在所有应用程序模型都继承的基础模型类上定义 <code v-pre>newCollection</code> 方法。</p>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/eloquent-collectionsmd/16704" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/el...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/eloquent-collectionsmd/16704" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/el...</a></p>
</blockquote>
</div></template>


